package com.ocean.dynamic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.cn/problems/pascals-triangle-ii/
 * 杨辉三角形
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2023/03/13 <br>
 */
public class PascalsTriangleII {

    public static void main(String[] args) {
        PascalsTriangleII triangle = new PascalsTriangleII();
        List<Integer> row = triangle.getRow(1);
        System.out.println(row);
    }

    public List<Integer> getRow(int rowIndex) {
        List<Integer> res = new ArrayList<>();
        res.add(0, 1);

        for (int i = 1; i <= rowIndex; i++) {
            for (int j = i; j >= 0; j--) {
                if (j == 0) {
                    continue;
                }
                if (j == i) {
                    res.add(j, 1);
                    continue;
                }
                res.set(j, res.get(j - 1) + res.get(j));
            }
        }
        return res;
    }

}
